package org.lifeaddict.sybill;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.view.MenuItem;
import android.widget.EditText;
 
public class MainActivity extends Activity implements OnClickListener{
 
	private LinkedHashMap<String, HeaderInfo> myDay = new LinkedHashMap<String, HeaderInfo>();
	private ArrayList<HeaderInfo> dayList = new ArrayList<HeaderInfo>();
	private ArrayList<String> loginDetails = new ArrayList<String>();
	private MyListAdapter listAdapter;
	private ExpandableListView myList;
	private ArrayList<DetailInfo> details = new ArrayList<DetailInfo>();
	private ArrayList<ArrayList<DetailInfo>> allLvs = new ArrayList<ArrayList<DetailInfo>>();
	private Boolean deleteIt = true;
	
	private DataSource dataSource;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		onCreate();
 	}
	
	public void onCreate() {
		setContentView(R.layout.activity_main);
		dataSource = new DataSource(this);
		dataSource.open();
		//get reference to the ExpandableListView
		myList = (ExpandableListView) findViewById(R.id.myList);
		//create the adapter by passing your ArrayList data
		listAdapter = new MyListAdapter(MainActivity.this, dayList);
		//attach the adapter to the list
		myList.setAdapter(listAdapter);  
		//listener for child row click
		myList.setOnChildClickListener(myListItemClicked);
		//listener for group heading click
        myList.setOnGroupClickListener(myListGroupClicked); 
        if (deleteIt) {
        	dataSource.deleteAll();
        	deleteIt = false;
        }
        //deleteAll();
		// storeData();
		loadData();
		dataSource.close();
		expandAll();
	}
	
	
 
	//method to expand all groups
	private void expandAll() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++){
			myList.expandGroup(i);
		}
	}
  
	//method to collapse all groups
	//private void collapseAll() {
		//int count = listAdapter.getGroupCount();
		//for (int i = 0; i < count; i++){
			//myList.collapseGroup(i);
		//}
	//}
 
	//load some initial data into out list
 	private void loadData(){
 		details = dataSource.getAllDetails();
 		while(!details.isEmpty()) {
 			addLV(details.remove(0));
 		}
 	}
 	
 	private void storeData() {
 		dataSource.createDetail("IT Einsatz in der Logistik","HS Umweltschutz",
 				"Mittwoch","5.Juni","9:00 - 10:00");
 		dataSource.createDetail("Beschaffungslogistik","HS Umweltschutz",
 				"Montag","3.Juni","10:00 - 16:15");
 		dataSource.createDetail("Blablaba in Huiii","HS Umweltschutz",
 				"Mittwoch","5.Juni","11:00 - 14:00");
 		dataSource.createDetail("Blablaba in Huiii","HS Umweltschutz",
 				"Mittwoch","12.Juni","20:00 - 21:00");
 	}
 	
 	//our child listener
 	private OnChildClickListener myListItemClicked =  new OnChildClickListener() { 
 		public boolean onChildClick(ExpandableListView parent, View v,
 				int groupPosition, int childPosition, long id) {
    
 			//get the group header
 			HeaderInfo headerInfo = dayList.get(groupPosition);
 			//get the child info
 			DetailInfo detailInfo =  headerInfo.getLvList().get(childPosition);
 			//display it or do something with it
 			Toast.makeText(getBaseContext(), "Clicked on Detail " + headerInfo.getName()
 					+ "/" + detailInfo.getName(), Toast.LENGTH_LONG).show();
 			return false;
 		} 
 	};
  
 	//our group listener
 	private OnGroupClickListener myListGroupClicked =  new OnGroupClickListener() {
 		public boolean onGroupClick(ExpandableListView parent, View v,
 				int groupPosition, long id) {
    
 			//get the group header
 			//HeaderInfo headerInfo = dayList.get(groupPosition);
 			//display it or do something with it
 			//header click!!!  
 			return false;
 		}   
 	};
 
 	//here we maintain our lvs in various dates
 	private int addLV(DetailInfo detail){
 		String lv;
 		int groupPosition = 0;
   
 		//check the hash map if the group already exists
 		HeaderInfo headerInfo = myDay.get(detail.getDate());
 		//add the group if doesn't exists
 		if(headerInfo == null){
 			headerInfo = new HeaderInfo();
 			headerInfo.setName(detail.getDay());
 			myDay.put(detail.getDate(), headerInfo);
 			dayList.add(headerInfo);
 		}
 
 		//get the children for the group
 		ArrayList<DetailInfo>lvList = headerInfo.getLvList();
 
 		//create a new child and add that to the group
 		DetailInfo detailInfo = new DetailInfo();
 		lv = detail.getName();
 		if (lv.length() >= 40)
 			lv = lv.substring(0,38)+"...";
 		detailInfo.setName(lv);	  
 		detailInfo.setTime(detail.getTime());
 		detailInfo.setLocation(detail.getLocation());
 		detailInfo.setAlarmTime(detail.getTime());
 		
 		lvList.add(detailInfo);
 		headerInfo.setLvList(lvList);
 		headerInfo.setDate(detail.getDate());
 
 		//find the group position inside the list
 		groupPosition = dayList.indexOf(headerInfo);
 		//lvList.clear();
 		allLvs.add(lvList);
 		return groupPosition;
 	}
 
 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 		super.onCreateOptionsMenu(menu);
 		getMenuInflater().inflate(R.menu.main, menu);
 		return true;
 	}
 	
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
 		switch(item.getItemId()) {
 	    case R.id.action_login:
 	    	setContentView(R.menu.login);
 	        return true;
 	    case R.id.action_settings:
 	    	//do something
 	    	return true;
 	 	}
		return false;
 	}
 	
 	public ArrayList<String> login() {
 		EditText etUser = (EditText)findViewById(R.id.editUser);
 		loginDetails.add(etUser.getText().toString());
 		
 		EditText etPw = (EditText)findViewById(R.id.editPw);
 		loginDetails.add(etPw.getText().toString());
 		System.out.println("User:"+loginDetails.get(0)+" / PW:"+loginDetails.get(1));
 		return loginDetails;
 	}

 	@Override
 	public void onClick(View view) {
 		//auto method
 	}
 	
 	public void deleteAll() {
 		// dataSource.deleteAll();
 		for(int i = 0;i < allLvs.size();i++)
 			allLvs.get(i).clear();
 		onCreate();
 	}
 	
 	public void onClickBtn(View v)
    {
 		ArrayList<String> toastMsg = new ArrayList<String>();
		toastMsg = login();

		new GetMuOnlineData(this.getBaseContext()).execute(toastMsg.get(0), toastMsg.get(1));
		
		deleteAll();
        //Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
    } 
 	
 	public void onClickDelete(View v)
    {
 		deleteAll();
 		Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
    }
}